package cn.kgc.springmvc.day04.controller;

import cn.kgc.springmvc.day04.entity.User;
import cn.kgc.springmvc.day04.response.ResponseResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/7/23
 * @description 如何实现该类下的接口 在访问时必须是一个已经认证通过的用户才能访问
 * <p>
 * 1.  springmvc中的拦截器
 * 2.  cookie+session的会话保持机制
 * <p>
 * 思路分析:
 * 1.  认证的模块    user/login    username  password
 * 2.  用户认证通过   标识当前用户时一个认证通过的用户  session   userInfo
 *
 * springmvc中如何实现异常的处理
 * 1. 异常局部处理方案     只对所在的类有效
 * 2. 异常全局处理方案     对项目中的任何模块抛出的异常均有效
 */
@RestController
@RequestMapping("user")
public class UserController {


    // 用户认证接口
    @GetMapping("login")
    public ResponseResult login(String username, String password, HttpSession session) {
        ResponseResult responseResult = new ResponseResult();

        if ("tom".equals(username) && "123456".equals(password)) {
            responseResult.setCode(0);
            responseResult.setMessage("登录成功");
            // 存储用户的认证信息   用于后续的非法请求的拦截
            session.setAttribute("username", username);
        } else {
            responseResult.setCode(110);
            responseResult.setMessage("用户名或密码错误");
        }

        return responseResult;
    }


    // 查询用户列表   /user/list
    @GetMapping("list")
    public ResponseResult userList() {

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMessage("查询成功");

        User user = new User();
        user.setId(1);
        user.setAge(20);
        user.setUsername("tom");
        user.setPassword("123456");

        List<User> users = Arrays.asList(user, user, user, user);

        responseResult.setData(users);

        return responseResult;
    }

    // 添加用户
    @PostMapping("add")
    public ResponseResult addUser(@RequestBody User user) {

        System.out.println("user = " + user);

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMessage("添加成功");

        return responseResult;
    }

    // 删除用户
    @DeleteMapping("delete/{id}")
    public ResponseResult deleteUser(@PathVariable("id") Integer id) {

        ResponseResult responseResult = new ResponseResult();


        if (id < 0) {
            throw new RuntimeException("参数异常");
        }


        responseResult.setCode(0);
        responseResult.setMessage("删除成功");

        return responseResult;
    }

    // 修改用户
    @PutMapping("update")
    public ResponseResult updateUser(@RequestBody User user) {

        System.out.println("user = " + user);

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMessage("修改成功");

        return responseResult;
    }


    // springmvc的异常处理方案
    // @ExceptionHandler(RuntimeException.class)
    // public ResponseResult  runtimeExceptionHandler(Exception e){
    //
    //     ResponseResult responseResult = new ResponseResult();
    //     responseResult.setCode(110);
    //     responseResult.setMessage("删除失败");
    //
    //     return  responseResult;
    // }

}
